<?php
/**
 * @package Facebook Like Box for PrestaShop
 * @version v1.0
 * @author Janos Biro
 * @copyright (C) 2010- WebshopWorks
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
**/

class FacebookLikeBox extends Module{
    
    private $_html = '';
    private $_postErrors = array();
    public function __construct()
    {
        $this->name = 'facebooklikebox';
        parent::__construct();

        $this->tab = 'WebshopWorks Modules';
        $this->version = '1.0';
        $this->displayName = $this->l('Facebook Like Box');
        $this->description = $this->l('The Like Box is a social plugin that enables Facebook Page owners to attract and gain Likes from their own website.');
    }

    public function install()
    {
        if (!parent::install())
            return false;

        if (!$this->registerHook('rightColumn') OR !$this->registerHook('leftColumn'))
            return false;

        Configuration::updateValue($this->name.'_pageurl', 'http://www.facebook.com/platform');
        Configuration::updateValue($this->name.'_width', '191');
        Configuration::updateValue($this->name.'_height', '343');
        Configuration::updateValue($this->name.'_connect', '9');
        Configuration::updateValue($this->name.'_stream', 'false');
        Configuration::updateValue($this->name.'_header', 'false');
        Configuration::updateValue($this->name.'_nmbr', '1');
        Configuration::updateValue($this->name.'_topbgc', 'E0E0E2');
        Configuration::updateValue($this->name.'_toptxtc', '990000');
        Configuration::updateValue($this->name.'_topbrdc', '888888');
        Configuration::updateValue($this->name.'_botbgc', 'F0F0F2');
        Configuration::updateValue($this->name.'_bottxtc', '990000');
        Configuration::updateValue($this->name.'_usrtxtc', '990000');
        return true;
    }
    
    private function _postValidation()
    {
        if (!Validate::isFloat(Tools::getValue('module_width')))
            $this->_postErrors[] = $this->l('Sorry but module width can be just whole number!');
//        if (!Validate::isFloat(Tools::getValue('module_height')))
//            $this->_postErrors[] = $this->l('Sorry but module height can be just whole number!');
//        if (!Validate::isFloat(Tools::getValue('product_width')))
//            $this->_postErrors[] = $this->l('Sorry but product width can be just whole number!');
//        if (!Validate::isFloat(Tools::getValue('product_height')))
//            $this->_postErrors[] = $this->l('Sorry but product height can be just whole number!');
//        if (!Validate::isFloat(Tools::getValue('scrolling_time')))
//            $this->_postErrors[] = $this->l('Sorry but scrolling time can be just whole number!');
//        if (!Validate::isFloat(Tools::getValue('nmbr_prod')))
//            $this->_postErrors[] = $this->l('Sorry but number of products can be just whole number!');
//        if (!Validate::isFloat(Tools::getValue('prd_disp_drop')))
//            $this->_postErrors[] = $this->l('Sorry but there is am error in dropdown list!');
    }

    public function generateCSS()
    {
      $file=fopen("../modules/".$this->name."/css/facebook.css","w") or $this->_postErrors[] = "Unable to create css file!";
      $css = '.fan_box
.full_widget 
.connect_top {
	border: none;
	background-color: #'.Tools::getValue('topbgc').';
	border-bottom: 1px solid;
	border-color: #'.Tools::getValue('topbrdc').';
}
.fan_box
.full_widget {
	border: none;
}
.connect_top a {
	color: #'.Tools::getValue('toptxtc').';
}
.connect_top a:hover {
	color: #CC0000;	
}
.fan_box
.connections {
	background-color: #'.Tools::getValue('botbgc').';
	border: none;
	color: #'.Tools::getValue('bottxtc').';
	height:'.Tools::getValue('module_height').'px;
}
.fan_box
.connections_grid
.grid_item
.name {
	color: #'.Tools::getValue('usrtxtc').'; 
}';

      fwrite($file,$css);
      fclose($file);
    }


    public function getContent()
    {
        $this->_html .= '<h2>'.$this->displayName.'</h2>';
        if (Tools::isSubmit('submit'))
        {
            $this->_postValidation();
            $this->generateCSS();
            if (!sizeof($this->_postErrors))
                {
                Configuration::updateValue($this->name.'_nmbr', Configuration::get($this->name.'_nmbr') + 1);
                Configuration::updateValue($this->name.'_pageurl', Tools::getValue('pageurl'));
                Configuration::updateValue($this->name.'_width', Tools::getValue('module_width'));
                Configuration::updateValue($this->name.'_height', Tools::getValue('module_height'));
                Configuration::updateValue($this->name.'_connect', Tools::getValue('connect'));
                Configuration::updateValue($this->name.'_stream', (Tools::getValue('stream') ? 'true' : 'false'));
                Configuration::updateValue($this->name.'_header', (Tools::getValue('header') ? 'true' : 'false'));
                Configuration::updateValue($this->name.'_topbgc', Tools::getValue('topbgc'));
                Configuration::updateValue($this->name.'_toptxtc', Tools::getValue('toptxtc'));
                Configuration::updateValue($this->name.'_topbrdc', Tools::getValue('topbrdc'));
                Configuration::updateValue($this->name.'_botbgc', Tools::getValue('botbgc'));
                Configuration::updateValue($this->name.'_bottxtc', Tools::getValue('bottxtc'));
                Configuration::updateValue($this->name.'_usrtxtc', Tools::getValue('usrtxtc'));
                $this->_html .= '<div class="conf confirm">'.$this->l('Settings updated').'</div>';
                }else
                {
                    foreach ($this->_postErrors AS $err)
                    {
                        $this->_html .= '<div class="alert error">'.$err.'</div>';
                    }
                }
        }

        $this->_displayForm();

        return $this->_html;
    }

    private function _displayForm()
    {       
        $this->_html .= '
        <form action="'.$_SERVER['REQUEST_URI'].'" method="post">
            <fieldset>
                <legend><img src="../img/admin/cog.gif" alt="" class="middle" />'.$this->l('Settings').'</legend>
                    <label style="width:220px; padding-right:10px;">'.$this->l('Facebook Page URL').'</label>
                    <div class="margin-form">
                        <input type="text" name="pageurl" value="'.Configuration::get($this->name.'_pageurl').'" />
            					<p class="clear">'.$this->l('The URL of the Facebook Page for this Like box.').'</p>
                    </div>
                    
                    <label style="width:220px; padding-right:10px;">'.$this->l('Module width (px)').'</label>
                    <div class="margin-form">
                        <input type="text" name="module_width" value="'.Configuration::get($this->name.'_width').'" />
            					<p class="clear">'.$this->l('The width of the plugin in pixels.').'</p>
                    </div>
                    
                    <label style="width:220px; padding-right:10px;">'.$this->l('Module height (px)').'</label>
                    <div class="margin-form">
                        <input type="text" name="module_height" value="'.Configuration::get($this->name.'_height').'" />
            					<p class="clear">'.$this->l('The height of the plugin in pixels.').'</p>
                    </div>
                    
                    <label style="width:220px; padding-right:10px;">'.$this->l('Connections').'</label>
                    <div class="margin-form">
                        <input type="text" name="connect" value="'.Configuration::get($this->name.'_connect').'" />
            					<p class="clear">'.$this->l('Show a sample of this many users who have liked this Page.').'</p>
                    </div>

            				<label style="width:220px; padding-right:10px;">'.$this->l('Stream').'</label>
            				<div class="margin-form">
            					<input type="checkbox" name="stream" value="'.(Tools::getValue('stream', Configuration::get($this->name.'_stream')) ? "true" : "false").'"' .
            					(Tools::getValue('stream', Configuration::get($this->name.'_stream')) == "true" ? ' checked="checked"' : '') . ' />
            					<p class="clear">'.$this->l('Show the profile stream for the public profile.').'</p>
            				</div>

            				<label style="width:220px; padding-right:10px;">'.$this->l('Header').'</label>
            				<div class="margin-form">
            					<input type="checkbox" name="header" value="'.(Tools::getValue('header', Configuration::get($this->name.'_header')) ? "true" : "false").'"' .
            					(Tools::getValue('header', Configuration::get($this->name.'_header')) == "true" ? ' checked="checked"' : '') . ' />
            					<p class="clear">'.$this->l('Show the "Find us on Facebook" bar at top. Only shown when either stream or connections are present.').'</p>
            				</div>

                    <label style="width:220px; padding-right:10px;">'.$this->l('Connect top backgroundcolor').'</label>
                    <div class="margin-form">
                        <input type="text" class="colorPicker" name="topbgc" value="'.Configuration::get($this->name.'_topbgc').'" />
                    </div>

                    <label style="width:220px; padding-right:10px;">'.$this->l('Connect top textcolor').'</label>
                    <div class="margin-form">
                        <input type="text" class="colorPicker" name="toptxtc" value="'.Configuration::get($this->name.'_toptxtc').'" />
                    </div>

                    <label style="width:220px; padding-right:10px;">'.$this->l('Connect top bordercolor').'</label>
                    <div class="margin-form">
                        <input type="text" class="colorPicker" name="topbrdc" value="'.Configuration::get($this->name.'_topbrdc').'" />
                    </div>

                    <label style="width:220px; padding-right:10px;">'.$this->l('Connections backgroundcolor').'</label>
                    <div class="margin-form">
                        <input type="text" class="colorPicker" name="botbgc" value="'.Configuration::get($this->name.'_botbgc').'" />
                    </div>

                    <label style="width:220px; padding-right:10px;">'.$this->l('Connections textcolor').'</label>
                    <div class="margin-form">
                        <input type="text" class="colorPicker" name="bottxtc" value="'.Configuration::get($this->name.'_bottxtc').'" />
                    </div>

                    <label style="width:220px; padding-right:10px;">'.$this->l('User names textcolor').'</label>
                    <div class="margin-form">
                        <input type="text" class="colorPicker" name="usrtxtc" value="'.Configuration::get($this->name.'_usrtxtc').'" />
                    </div>


            </fieldset>
            <br/>
            <input type="submit" name="submit" value="'.$this->l('Update').'" class="button" />
        </form>
        <link rel="stylesheet" media="screen" type="text/css" href="'._MODULE_DIR_.$this->name.'/css/colorpicker.css" />
        <script type="text/javascript" src="'._MODULE_DIR_.$this->name.'/js/colorpicker.js"></script>

        <script type="text/javascript">
            $(\'.colorPicker\').ColorPicker({
                    onSubmit: function(hsb, hex, rgb, el) {
                            $(el).val(hex);
                            $(el).ColorPickerHide();
                    },
                    onBeforeShow: function () {
                            $(this).ColorPickerSetColor(this.value);
                    }
            })
            .bind(\'keyup\', function(){
                    $(this).ColorPickerSetColor(this.value);
            });
        </script>
        ';
        
    }

    function hookRightColumn($params)
    {
      global $smarty;

      $smarty->assign(array(
                    $this->name.'_path' => _PS_BASE_URL_._MODULE_DIR_.$this->name.'/css/facebook.css%3F'.Configuration::get($this->name.'_nmbr'),
                    $this->name.'_pageurl' => Configuration::get($this->name.'_pageurl'),
                    $this->name.'_width' => Configuration::get($this->name.'_width'),
                    $this->name.'_height' => Configuration::get($this->name.'_height'),
                    $this->name.'_connect' => Configuration::get($this->name.'_connect'),
                    $this->name.'_stream' => Configuration::get($this->name.'_stream'),
                    $this->name.'_header' => Configuration::get($this->name.'_header'),
                    $this->name.'_auth' => "http://prestashop.webshopworks.com/"
                    
            ));
            return $this->display(__FILE__, 'facebooklikebox.tpl');

    }
    
    function hookLeftColumn($params)
    {
        return $this->hookRightColumn($params);
    } 
}